/**
 * @ author: 汪孟苹
 */
define(['jquery','bbcare'],function($,$bb){
	var servers = {};
	var URL = $bb.baseUrl("followapp");

	/**
	 * 处理基本信息数据
	 */
	servers.handleInfo = function(data){
		data.deliveryStatus = data.deliveryStatus == "1" ? "分娩" : data.deliveryStatus == "2" ? "未分娩" : "--";
		if( !data.deliveryDate ) data.deliveryDate = "--";
		if( !data.deliveryDay ) data.deliveryDay = "--";
		return data;
	}

	/**
	 * 请求备孕妈妈BMI值
	 */
	servers.ajaxPrepregnancyBmi = function(params,callback){
		$bb.ajax({
			url: URL + "webapi/service/qryPregnancyWeightList",
			data:JSON.stringify({
				data : {
					"userId":params.info.userId,
				}
			}),
			success: function(data){
				if(data.success){
					callback(data.data,params);
				}else{
					$bb.toastShow(data.msg ? data.msg : "查询备孕妈妈BMI值记录出错！");
				}
			}
		});
	}

	/**
	 * 处理备孕妈妈bmi值
	 */
	servers.hanleBmiData = function(list){
		for (var i = 0; i < list.length; i++) {
			if( list[i].bmi ){
				if( list[i].bmi < 18.5 ){
					list[i].className= "text-green";
				}else if( list[i].bmi < 24.9 ){
				}else if( list[i].bmi < 29.9 ){
					list[i].className= "text-orange";
				}else{
					list[i].className= "text-red";
				}
			}
		}
		return {"list":list};
	}

	/**
	 * 查询胎动记录
	 */
	servers.ajaxFetalmovement = function(params,callback){
		$bb.ajax({
			url: URL + "webapi/service/qryFetalmovementList",
			data:JSON.stringify({
				data : {
					"userId":params.info.userId,
					"queueId":params.info.queueId,
				}
			}),
			success: function(data){
				if(data.success){
					callback(data.data,params);
				}else{
					$bb.toastShow(data.msg ? data.msg : "查询胎动记录出错！");
				}
			}
		});
	}

	/**
	 * 将答案赋值给对应的天
	 */
	var dayArrSetValue = function(obj,arr){
		if( obj.day == 0 ) arr[0] = obj;
		if( obj.day == 1 ) arr[1] = obj;
		if( obj.day == 2 ) arr[2] = obj;
		if( obj.day == 3 ) arr[3] = obj;
		if( obj.day == 4 ) arr[4] = obj;
		if( obj.day == 5 ) arr[5] = obj;
		if( obj.day == 6 ) arr[6] = obj;
	}

	/**
	 * 获取fetalmovementList
	 */
	var getFetalmovementList = function(obj){
		var arr = new Array(7);
		for (var i = 0; i < obj.fetalmovementList.length; i++) {
			dayArrSetValue(obj.fetalmovementList[i],arr);
		}
		for (var i = 0; i < arr.length; i++) {
			var obj1 = { "day":i };
			if( !arr[i] ) arr[i] = obj1; 
		}
		obj.fetalmovementList = arr;
		return obj;
	}

	/**
	 * 将答案赋值给对应的月份
	 */
	var weekArrSetValue = function(obj,arr){
		if( obj.pregnancyWeek == 28 ) arr[0] = getFetalmovementList(obj);
		if( obj.pregnancyWeek == 29 ) arr[1] = getFetalmovementList(obj);
		if( obj.pregnancyWeek == 30 ) arr[2] = getFetalmovementList(obj);
		if( obj.pregnancyWeek == 31 ) arr[3] = getFetalmovementList(obj);
		if( obj.pregnancyWeek == 32 ) arr[4] = getFetalmovementList(obj);
		if( obj.pregnancyWeek == 33 ) arr[5] = getFetalmovementList(obj);
		if( obj.pregnancyWeek == 34 ) arr[6] = getFetalmovementList(obj);
		if( obj.pregnancyWeek == 35 ) arr[7] = getFetalmovementList(obj);
		if( obj.pregnancyWeek == 36 ) arr[8] = getFetalmovementList(obj);
		if( obj.pregnancyWeek == 37 ) arr[9] = getFetalmovementList(obj);
		if( obj.pregnancyWeek == 38 ) arr[10] = getFetalmovementList(obj);
		if( obj.pregnancyWeek == 39 ) arr[11] = getFetalmovementList(obj);
		if( obj.pregnancyWeek == 40 ) arr[12] = getFetalmovementList(obj);
		if( obj.pregnancyWeek == 41 ) arr[13] = getFetalmovementList(obj);
	}

	/**
	 * 填充
	 */
	var fill = function(){
		var arr = new Array(7);
		for (var i = 0; i < arr.length; i++) {
			arr[i] = {},
			arr[i].day = i;
		}
		return arr;
	}

	/**
	 * 创建数组day
	 */
	var dayArr = function(){
		var arr = new Array(7);
		for (var i = 0; i < arr.length; i++) {
			arr[i] = (i+1).toString();
		}
		return arr;
	}

	/**
	 * 处理胎动记录数据
	 */
	servers.hanleFmData = function(list){
		var arr = new Array(14);
		for (var i = 0; i < list.length; i++) {
			weekArrSetValue(list[i],arr);
		}
		for (var i = 0; i < arr.length; i++) {
			var obj = { 
				"pregnancyWeek":i+28,
				"fetalmovementList":fill()
			};
			if( !arr[i] ) arr[i] = obj; 
		}
		return {
			"list":arr,
			"days":dayArr()
		};
	}

	/**
	 * 请求听课列表
	 */
	servers.ajaxCourse = function(params,callback){
		$bb.ajax({
			url: URL + "webapi/service/qryCourseRecordList",
			data:JSON.stringify({
				data : {
					"userId":params.info.userId,
					"queueId":params.info.queueId,
					"flag":"1",
					"rows":params.rows,
					"page":params.page,
				}
			}),
			success: function(data){
				if(data.success){
					callback(data,params);
				}else{
					$bb.toastShow(data.msg ? data.msg : "请求听课列表出错！");
				}
			}
		});
	}

	return servers;
});